﻿using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Read.API.Command.RoleCommand;
using MediatR;

namespace B.S.BaseData.Read.API.CommandHandler.RoleHandler
{
    public class GetRoleCommandHandler : IRequestHandler<GetRoleCommand, APIResult<List<Role>>>
    {
        private readonly IBaseRepository<Role> roleRepos;

        public GetRoleCommandHandler(IBaseRepository<Role> roleRepos)
        {
            this.roleRepos = roleRepos;
        }

        /// <summary>
        /// 角色显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<List<Role>>> Handle(GetRoleCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<Role>> result = new APIResult<List<Role>>();
            var list = roleRepos.GetAll().Where(x => x.IsDeleted == false);
            if (list == null)
            {
                result.Code = APIEnum.Failed;
                result.Msg = "查询失败";
            }
            else
            {
                result.Code = APIEnum.Success;
                result.Msg = "查询成功";
                result.Data = list.ToList();
            }
            return Task.FromResult(result);
        }
    }
}
